package cn.zl.ymcc.service.impl;

import cn.zl.ymcc.dto.CourseSaveDto;
import cn.zl.ymcc.dto.StationDto;
import cn.zl.ymcc.entity.*;
import cn.zl.ymcc.mapper.CourseMapper;
import cn.zl.ymcc.result.Result;
import cn.zl.ymcc.service.*;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zl
 * @since 2025-10-21
 */

@Tag(name = "Course服务实现")
@Slf4j
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseDetailService courseDetailService;
    @Autowired
    private CourseResourceService courseResourceService;
    @Autowired
    private CourseMarketService courseMarketService;
    @Autowired
    private CourseTeacherService courseTeacherService;

    @Override
    public void onLineCourse(List<Long> ids) {

        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", ids);
        // 动态设置要更新的字段
        updateWrapper.set("status", new Byte("1"));
        update(updateWrapper);

        //发送消息
        sendMessage();


    }

    private void sendMessage() {
        StationDto stationDto = new StationDto();
        stationDto.setContent("课程审核通过，请及时发布课程");
        stationDto.setType("1");
        stationDto.setTitle("课程审核通过");
        List<Long> list = Arrays.asList(1L, 2L, 3L, 4L);
        stationDto.setUserId(list);
        stationDto.setIsread(0);
        stationDto.setSendTime(LocalTime.now().toString());
        SendResult sendResult = rocketMQTemplate.syncSend("course:station", stationDto);
        log.info("结果是：{}", sendResult);
        System.out.println("发送成功"+sendResult);
    }


    @Override
    @Transactional
    public void insert(CourseSaveDto courseSaveDto) {

        //1.校验
        Course course = courseSaveDto.getCourse();
        CourseMarket courseMarket = courseSaveDto.getCourseMarket();
        CourseResource courseResource = courseSaveDto.getCourseResource();
        CourseDetail courseDetail = courseSaveDto.getCourseDetail();
        List<Long> teacherIds = courseSaveDto.getTeacharIds();

        //2.插入
        //2.1 插入课程基本信息
        List<Teacher> teachers = teacherService.listByIds(teacherIds);
        String teacherNames = teachers.stream().map
                (Teacher::getName).collect(Collectors.joining(",")).toString();
        course.setStatus(new Byte("0"));
        course.setTeacherNames(teacherNames);
        save(course);
        //2.2 插入课程详情信息
        courseDetail.setId(course.getId());
        courseDetailService.save(courseDetail);
        //2.3 插入课程营销信息
        courseMarket.setId(course.getId());
        courseMarketService.save(courseMarket);
        //2.4 插入课程资源信息
        courseResource.setCourseId(course.getId());
        courseResourceService.save(courseResource);
        //2.5 插入课程教师信息
        List<CourseTeacher> courseTeachers = teacherIds.stream().map(teacherId -> {
            CourseTeacher courseTeacher = new CourseTeacher();
            courseTeacher.setCourseId(course.getId());
            courseTeacher.setTeacherId(teacherId);
            return courseTeacher;
        }).collect(Collectors.toList());
        courseTeacherService.saveBatch(courseTeachers);
        //3.返回
    }
}
